<?php

class Core_Data_Generator_DefinitionInterpreter_Table {

    private $FieldCollection;
    private $MethodCollection;
    
    public $Name;
    public $Prefix;

    function __construct () {
        $this->FieldCollection = new Core_Data_SQL_DatabaseStructure_FieldCollection ();
        $this->MethodCollection = new Core_Data_SQL_DatabaseStructure_MethodCollection ();
    }

    /**
     * Gets the actual table name.
     *
     * The actual table name is [prefix]_[name], i.e. someprefix_sometable.
     *
     * @return string
     */
    function __toString () {
        return $this->Name . "_" . $this->Prefix;
    }

    public function fields () {
        return $this->FieldCollection;
    }

    public function methods () {
        return $this->MethodCollection;
    }









// OLD CODE
    private $xmlTable;

    /**
     *
     * @param SimpleXMLElement $xmlTable
     * @exception More than one 'field' element in a 'table' element is set to hold a key. <i>Exception code 107</i>.
     */
    function __construct ( $xmlTable ) {
        $this->xmlTable = $xmlTable;

        // Ensure that no more than one field is set to hold a key.
        $i = 0;
        foreach ( $this->getFields() as $objField ) {
            if ( $objField->isKey() ) {
                $i++;
            }
        }
        if ( $i > 1 ) {
            throw new Exception ( "More than one 'field' element in a 'table' element is set to hold a key.", 107 );
        }
    }

    /**
     * Gets the name of the table class.
     *
     * @return boolean|string The name of the class as string if set, <kbd>false</kbd> if not.
     */
    public function getClassName () {
        if ( isset( $this->xmlTable->attributes()->classname ) ) {
            return (string) $this->xmlTable->attributes()->classname;
        } else {
            return false;
        }
    }

    /**
     * Gets the filename for the table class.
     *
     * @return boolean|string The filename of the class as string if set, <kbd>false</kbd> if not.
     */
    public function getClassFilename () {
        if ( isset( $this->xmlTable->attributes()->classfilename ) ) {
            return (string) $this->xmlTable->attributes()->classfilename;
        } else {
            return false;
        }
    }

    /**
     * Gets the package name for the class.
     *
     * This is used by PHPDoc.
     *
     * @return boolean|string The package name for the class as string if set, <kbd>false</kbd> if not.
     */
    public function getPackageName () {
        if ( isset( $this->xmlTable->attributes()->packagename ) ) {
            return (string) $this->xmlTable->attributes()->packagename;
        } else {
            return false;
        }
    }

    /**
     * Gets the subpackage name for the class.
     *
     * This is used by PHPDoc.
     *
     * @return boolean|string The subpackage name for the class as string if set, <kbd>false</kbd> if not.
     */
    public function getSubpackageName () {
        if ( isset( $this->xmlTable->attributes()->subpackagename ) ) {
            return (string) $this->xmlTable->attributes()->subpackagename;
        } else {
            return false;
        }
    }

    /**
     * Checks if the table has a corresponding record class.
     *
     * @return boolean
     */
    public function holdsRecord () {
        if ( isset( $this->xmlTable->record ) ) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Gets the record of a table.
     *
     * @return Core_Data_Generator_XMLInterpreter_Record
     */
    public function getRecord () {
        if ( isset( $this->xmlTable->record ) ) {
            return new Core_Data_Generator_XMLInterpreter_Record( $this->xmlTable->record );
        }
    }

}

?>